<?php

namespace App\Http\Controllers\Customer;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Base\BaseController;

class AjaxCustomerDataController extends BaseController

{


    /**
     * 修改店铺信息
     *
     * @param Request $request
     *
     * @return array
     */
    public function customerInfoSave(Request $request)
    {
        $param = $request->all();
        foreach ($param['shopInfo'] as $value) {
            $map['shop_id'] = $value['shop_id'];
            unset($value['shop_id']);
            DB::table('shop_master')->where($map)->update($value);
        }
        return $this::showReturnCode(1001, [], trans('symessages.ajax_data.update_success'));
    }

    /**
     * 修改密码
     *
     * @param Request $request
     *
     * @return array
     */
    public function changePwd(Request $request)
    {
        $user_id = Cookie::get('user_id');
        $paramData = $request->all();

        $validator = Validator::make($paramData, [
            'password' => 'required',
        ])->validate();

        $ret = DB::table('user_master')->where('user_id', '=', $user_id)->update(['password' => $paramData['password']]);
        if ($ret != false) {
            return $this::showReturnCode(1001, [], trans('symessages.ajax_data.update_success'));
        } else {
            return $this::showReturnCode(1023, [], trans('symessages.ajax_data.update_fail'));
        }
    }

    /**
     * 加入购物车功能
     * @param Request $request
     *
     * @return array
     */
    public function goodsToCat(Request $request)
    {
        $paramData = $request->all();
        $user_id = Cookie::get('user_id');
        $saveData = [];
        foreach ($paramData['goodsList'] as $val){
            $da['user_id'] = $user_id;
            $da['goods_id'] = $val['goods_id'];
            $da['goods_price'] = $val['price'];
            $da['goods_model_id'] = $val['goods_model_id'];
            $da['size'] = $val['size_name'];
            $da['color'] = $val['color_name'];
            $da['goods_name'] = $val['goods_name'];
            $da['goods_num'] = $val['goods_num'];
            $da['create_time'] = toDate(time());
            $is_zai = DB::table('user_cart')->where(['goods_id'=>$val['goods_id'],'goods_model_id'=>$val['goods_model_id']])->first();
            if($is_zai != false){
                DB::table('user_cart')->where(['goods_id'=>$val['goods_id'],'goods_model_id'=>$val['goods_model_id']])->increment('goods_num',$val['goods_num']);
            }else{
                $saveData[] = $da;
            }
        }
        $ret = DB::table('user_cart')->insert($saveData);
        if($ret != false){
            return $this::showReturnCode(1001, [], trans('symessages.ajax_data.install_success'));
        }else{
            return $this::showReturnCode(1023, [], trans('symessages.ajax_data.insert_fail'));
        }
    }

    /**
     * 删除购物车商品
     * @param Request $request
     *
     * @return array
     */
    public function delCartGoods(Request $request){
        $cart_id = $request->input('cart_id');
        $ret = DB::table('user_cart')->where(['cart_id'=>$cart_id])->delete();
        return $this::showReturnCode(1001, [], trans('symessages.ajax_data.del_success'));
    }

    /**
     * 用户下单
     * @param Request $request
     *
     * @return array
     */
    function saveOrder(Request $request)
    {

        $user_id = Cookie::get('user_id');
        $user_name = Cookie::get('user_name');

        $corp_id = Cookie::get('corp_id');

        $paramData = $request->all();

        $validator = Validator::make($paramData, [
            'order_id' => 'required|unique:user_order|max:255',

        ])->validate();

        $shopInfo = DB::table('shop_master')->where(['shop_id'=>$paramData['shop_id']])->first();

        $orderData['user_id'] = $user_id;
        $orderData['user_name'] = $user_name;
        $orderData['order_id'] = $paramData['order_id'];
        $orderData['money'] = 0;
        $orderData['consignation_date'] = empty($paramData['weituo_date']) ? toDate(time()) : $paramData['weituo_date'];
        $orderData['create_time'] = toDate(time());
        $orderData['send_goods_time'] = empty($paramData['goods_date']) ? toDate(time()) : $paramData['goods_date'];

        $orderData['name'] = $shopInfo->shop_name;
        $orderData['shop_id'] = $shopInfo->shop_id;
        $orderData['corp_id'] = $corp_id;

        $orderData['add'] = $shopInfo->shop_adress;
        $orderData['add_alias'] = $shopInfo->shop_biru_name;
        $orderData['tel'] = $shopInfo->shop_telephone_number;
        $orderData['postal_code'] = $shopInfo->shop_postcode;
        $orderData['fax'] = $shopInfo->shop_fax_number;
        DB::beginTransaction();
        $ret = DB::table('user_order')->insert($orderData);

        if ($ret != false) {
            $listData = $paramData['goods_list'];
            $detailData = [];
            foreach ($listData as $value) {
                if($value['goods_num'] > 0) {
                    $gData['order_id'] = $orderData['order_id'];
                    $gData['goods_id'] = $value['goods_id'];
                    $gData['goods_num'] = $value['goods_num'];
                    $gData['goods_name'] = $value['goods_name'];
                    $gData['goods_price'] = $value['goods_price'];
                    $gData['size'] = $value['size'];
                    $gData['color'] = $value['color'];
                    $gData['model_id'] = $value['goods_model_id'];
                    $gData['deliver_time'] = toDate(time());
                    $gData['create_time'] = toDate(time());
                    $orderData['money'] += $gData['goods_num'] * $gData['goods_price'];
                    $detailData[] = $gData;
                }
            }
            if (empty($detailData)) {
                DB::rollBack();
                return $this::showReturnCode(1033, [], trans('symessages.ajax_data.detail_data'));
            }

             DB::table('user_order')->where(['order_id'=>$orderData['order_id']])->update(['money'=>$orderData['money']]);
            $ret2 = DB::table('user_order_detailed')->insert($detailData);
            if($paramData['cart_buy'] == 'delete'){
                DB::table('user_cart')->where(['user_id'=>$user_id])->delete();
            }
            if ($ret2 === true) {
                DB::commit();
                return $this::showReturnCode(1001, [], trans('symessages.ajax_data.insert_success'));
            }
        } else {
            return $this::showReturnCode(1031);
        }

    }

}
